<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-09-08 10:34:33
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\System;

use App\Http\Services\Service;
use App\Models\MenuMetaModel;
use App\Models\MenuModel;
use App\Repositories\MenuRepository;
use Illuminate\Http\Request;

class MenuService extends Service
    {

    public function __construct(MenuRepository $Repository)
        {
        $this->repository = $Repository;
        }
    public function list()
        {
        $data = $this->repository->getMenuList();
        return $data;
        }

    protected function save(Request $request)
        {
        $menu_data = $request->toArray();
        unset($menu_data['children']);
        $data       = $this->repository->autosave($menu_data);
        $data->sort = $data->id;
        $data->save();
        return $data;
        }

    protected function delete(Request $request)
        {
        return $this->repository->delete($request->ids);
        }

    public function role(Request $request)
        {
        return $this->repository->role($request->id);
        }

    protected function saveSort()
        {
        return array_filter(request()->input("sort"), function ($menu)
            {

            $menuModel            = $this->repository->whereId($menu['id'])->first();
            $menuModel->parent_id = $menu['pId'] ?? 0;
            $menuModel->sort      = $menu['sort'];
            return $menuModel->save();
            });

        }
    }